<template>
  <div>
    <el-row>
      <el-col :span="4" v-for="item in goods" :key="item.id">
        <el-card :body-style="{ padding: '0px' }" shadow="hover" class="md1">
          <img :src=item.photo class="image">
          <div style="padding: 14px;">
            <span>{{item.goodsName}}</span>
            <span>{{item.price}}</span>
            <div class="bottom clearfix">
              <el-button type="text" @click="loadgoodsDetail(item.id)">查看详情</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="商品名" :label-width="formLabelWidth">
          <el-input v-model="goodsDetail.goodsName" autocomplete="off" :disabled="false"></el-input>
        </el-form-item>
        <el-form-item label="商品封面" :label-width="formLabelWidth">
          <el-input v-model="goodsDetail.photo" autocomplete="off" :disabled="false"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input-number v-model="goodsDetail.price" :step="0.1" :min="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="数量" :label-width="formLabelWidth">
          <el-input-number v-model="goodsDetail.number" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input v-model="goodsDetail.summary" autocomplete="off" :disabled="false"></el-input>
        </el-form-item>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyGoods">确认修改</el-button>
        <el-button @click="deleteGoods">删除</el-button>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'MyMall',
    data() {
      return {
        goods: [],
        goodsDetail: [],
        dialogTableVisible: false,
        dialogFormVisible: false,
        formLabelWidth: '120px'
      }
    },
    mounted: function() {
      this.loadGoods()
    },
    methods: {
      loadGoods() {
        this.$axios.get('/findGoodsByMall').then(resp => {
          this.goods = resp.data
        })
      },
      loadgoodsDetail(key) {
        this.dialogFormVisible = true
        this.$axios.post('/findGoodsDetail', {
          id: key
        }).then(resp => {
          this.goodsDetail = resp.data
        })

      },
      modifyGoods() {
        this.$axios.post('/modifyGoods', {
          id: this.goodsDetail.id,
          businessId: this.goodsDetail.businessId,
          goodsName: this.goodsDetail.goodsName,
          photo: this.goodsDetail.photo,
          price: this.goodsDetail.price,
          category: this.goodsDetail.category,
          number: this.goodsDetail.number,
          summary: this.goodsDetail.summary,
          state: this.goodsDetail.state
        }).then(resp => {
          if (resp.data.code === 200) {
            alert("修改成功")
            this.dialogFormVisible = false
          } else {
            alert("修改失败")
          }
        })

      },
      deleteGoods(){
        this.$axios.post('/deleteGoods', {
          id: this.goodsDetail.id,
        }).then(resp => {
          if (resp.data.code === 200) {
            alert("删除成功")
            this.dialogFormVisible = false
          } else {
            alert("删除失败")
          }
        })
      }
    }
  }
</script>

<style>
  .md1 {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    height: 200px;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }
</style>
